package com.ag.answergames.service.user.impl;

import cn.hutool.http.HttpUtil;
import com.ag.answergames.entity.UserEntity;
import com.ag.answergames.mapper.user.UserMapper;
import com.ag.answergames.service.user.UserService;
import com.ag.answergames.util.SnowflakeIdWorkerController;
import com.ag.answergames.util.Tools;
import com.ag.answergames.vo.UserVO;
import com.ag.answergames.vo.common.Result;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class UserServiceImpl implements UserService {


    @Autowired
    UserMapper userMapper;


//    @Override
//    public Result userRegister(String phone, String verifyCode, String code) {
//            try
//            {
////                if (Tools.IsNullOrEmpty(code)) {
////                    return Result.failed ();
////                }
//
//                //1.得到解密code的网址
//                /**
//                 * https://api.weixin.qq.com/sns/oauth2/access_token接口得到的返回值为：
//                 *{
//                 *   "access_token": "",
//                 *   "expires_in": 7200,
//                 *   "refresh_token": "",
//                 *   "openid": "",
//                 *   "scope": ""
//                 * }
//                 *
//                 * https://api.weixin.qq.com/sns/jscode2session接口得到的返回值为：
//                 * {
//                 *   "session_key": "",
//                 *   "expires_in": 7200,
//                 *   "openid": "",
//                 *   "unionid": ""
//                 * }
//                 *
//                 */
//            /*String getTokenUrl = "https://api.weixin.qq.com/sns/oauth2/access_token?" +
//                    "appid=" + appid +
//                    "&secret=" + appsecret +
//                    "&code=" + code +
//                    "&grant_type=authorization_code";*/
//                String getTokenUrl = "https://api.weixin.qq.com/sns/jscode2session?" +
//                        "appid=" + "wx2687e5d495023188"
//                        + "&secret=" +"86ec9514fc22c5b1651a5f69fadd9eb1"
//                        + "&js_code=" + "0f3tdr000ESmQP1pL3000yarqH3tdr0X"
//                        + "&grant_type=authorization_code";
//
//                //2.获取解密的json
//                //这个属于发起请求并获取json数据（得引入hutool，即上面引入的第二个依赖）
//                String json= HttpUtil.createGet(getTokenUrl).execute().body();
//
//                //3.解析json
//                JSONObject jsonObject = JSONObject.parseObject (json);
//
//                System.out.println (jsonObject);
//                String openId = jsonObject.getString ("openid");
//
//                System.out.println (openId);
//                String sessionKey = jsonObject.getString ("session_key");
//                String expiresIn = jsonObject.getString ("expires_in");
//
////                if(Tools.IsNullOrEmpty(openId) ||
////                        Tools.IsNullOrEmpty(sessionKey) ||
////                        Tools.IsNullOrEmpty(expiresIn)){
////                    return Result.failed ();
////                }
//
//                UserEntity userEntity=new UserEntity ();
//                userEntity.setPhone (phone);
//                SnowflakeIdWorkerController snowflakeIdWorkerController = new SnowflakeIdWorkerController ();
//
//                Long id = snowflakeIdWorkerController.nextId ();
//                userEntity.setId (id);
//                UserVO userVO = new UserVO ();
////                QueryWrapper<UserEntity> wrapper = Wrappers.query ();
////                UserEntity userEntity1 = userMapper.selectOne (wrapper);
////                userVO.setOpenId (userEntity1.getOpenId ());
//                userVO.setId (userEntity.getId ());
//                userVO.setOpenId (openId);
//
////                QueryWrapper<UserEntity> wrapper = Wrappers.query ();
//                userMapper.insert (userEntity);
////                return Result.success (userVO);
//                Map<String,String> retMap=new HashMap<String, String> ();
//                retMap.put("openid",openId);
//                retMap.put("session_key",sessionKey);
//                retMap.put("expires_in",expiresIn);
//
//                return Result.success (userVO);
//
//
//            }
//
//            catch(Exception ex) {
////            System.out.println(ex.getMessage());
//                return Result.failed ();
//
//            }
//
//
//    }


    @Override
    public Result<UserVO> userRegister(String code, String verifyCode, String phone) {

        UserEntity userEntity=new UserEntity ();
        userEntity.setPhone (phone);
        userEntity.setOpenId (phone+"abc");
        SnowflakeIdWorkerController snowflakeIdWorkerController = new SnowflakeIdWorkerController ();
        userEntity.setId (snowflakeIdWorkerController.nextId ());
        UserVO userVO = new UserVO ();
        userVO.setOpenId (userEntity.getOpenId ());
        userVO.setId (userEntity.getId ());
        QueryWrapper<UserEntity> wrapper = Wrappers.query ();
//        wrapper=wrapper.eq ("open_id",userEntity.getOpenId ());
        userMapper.insert (userEntity);
        return Result.success (userVO);

    }

    @Override
    public Result<Integer> userEdit(UserVO userVO) {

        UserEntity userEntity=new UserEntity ();
        userEntity.setUserName (userVO.getUsername ());
        userEntity.setBirthday (userVO.getBirthday ());
        userEntity.setEmail (userVO.getEmail ());
        userEntity.setGender (userVO.getGender ());
        userEntity.setIsVip (userVO.getIsVip ());
        userEntity.setPhone (userVO.getPhone ());
        QueryWrapper<UserEntity> wrapper = Wrappers.query ();

        wrapper=wrapper.eq ("open_id",userVO.getOpenId ());
        return Result.success (userMapper.update (userEntity,wrapper));

    }

    @Override
    public Result<UserVO> userInfo(String openId) {

        UserEntity userEntity=new UserEntity ();
        userEntity.setOpenId (openId);

        QueryWrapper<UserEntity> wrapper = Wrappers.query ();

        return Result.success (userMapper.selectOne (wrapper));
    }
}
